package com.devframe.web.struts1.converters;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.beanutils.Converter;

/**
 * 
 *<pre><b><font color="blue">DateConverter</font></b></pre>
 *
 *<pre><b>&nbsp;--描述说明--</b></pre>
 * <pre></pre>
 * <pre>
 * <b>--样例--</b>
 *   DateConverter obj = new DateConverter();
 *   obj.method();
 * </pre>
 * JDK版本：JDK1.4
 * @author  <b></b>
 */
public class DateConverter implements Converter {
	/**时间格式化器*/
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	/**例子*/
	private static String sample = "2005-01-01 12:00:00.000";

	// private static int samplelength = sample.length();

	/**
	 * @param class0 Class
	 * @param object Object
	 * @return Object
	 */
	public Object convert(Class class0, Object object) {
		if (object == null) {
			return null;
		}
		if (object instanceof java.util.Date) {
			return object;
		}
		String str = "";
		if (!(object instanceof String)) {
			str = object.toString();
		}
		if (object instanceof String) {
			str = ((String) object).trim();
			if (str.equals("")) {
				return null;
			}
			str = str + sample.substring(str.length());
			try {
				return sdf.parse(str);
			} catch (ParseException ex) {
				// throw new ConversionException(str + " 不是有效的日期！");
				return null;
			}
		} else {
			// throw new ConversionException(object + " 不是一个字符串！");
			return null;
		}

	}

}
